//
//  SLCATransformLayerController.m
//  Objective-C
//
//  Created by S_Line on 2018/6/29.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLCATransformLayerController.h"

static CGFloat const bianchang = 50.0;

@interface SLCATransformLayerController ()

@property (nonatomic, weak) UIView *containerView;

@end

@implementation SLCATransformLayerController

- (void)initNavBar {
    self.navTitle = @"CATransformLayer";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initView];
}

- (void)initView {
    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, bianchang, bianchang)];
    self.containerView = containerView;
    [self.view addSubview:containerView];
    
    CATransform3D pt = CATransform3DIdentity;
    pt.m34 = -1.0 / 500.0;
    self.containerView.layer.sublayerTransform = pt;
    
    // set up the transform for cube 1 and add it
    CATransform3D ct1 = CATransform3DIdentity;
    ct1 = CATransform3DTranslate(ct1, -bianchang, 0, 0);
    CALayer *cube1 = [self cubeWithTransform:ct1];
    [self.containerView.layer addSublayer:cube1];
    
    // set up the transform for cube 1 and add it
    CATransform3D ct2 = CATransform3DIdentity;
    ct2 = CATransform3DTranslate(ct1, bianchang * 2, bianchang * 2, 0);
    ct2 = CATransform3DRotate(ct2, -M_PI_4, 1, 0, 0);
    ct2 = CATransform3DRotate(ct2, -M_PI_4, 0, 1, 0);
    CALayer *cube2 = [self cubeWithTransform:ct2];
    [self.containerView.layer addSublayer:cube2];
}

- (CALayer *)faceWithTransform:(CATransform3D)transform {
    // create cube face layer
    CALayer *face = [CALayer layer];
    face.frame = CGRectMake(-bianchang / 2.0, -bianchang / 2.0, bianchang, bianchang);
    
    // apply a random color
    CGFloat red = (rand() / (double)INT_MAX);
    CGFloat green = (rand() / (double)INT_MAX);
    CGFloat blue = (rand() / (double)INT_MAX);
    face.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0].CGColor;
    
    // apply the transform and return
    face.transform = transform;
    return face;
}

- (CALayer *)cubeWithTransform:(CATransform3D)transform {
    // create the cube layer
    CATransformLayer *cube = [CATransformLayer layer];
    
    // add cube face 1
    CATransform3D ct = CATransform3DMakeTranslation(0, 0, bianchang / 2.0);
    [cube addSublayer:[self faceWithTransform:ct]];
    
    // add cube face 2
    ct = CATransform3DMakeTranslation(bianchang / 2.0, 0, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 0, 1, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    
    // add cube face 3
    ct = CATransform3DMakeTranslation(0, -bianchang / 2.0, 0);
    ct = CATransform3DRotate(ct, M_PI_2, 1, 0, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    
    // add cube face 4
    ct = CATransform3DMakeTranslation(0, bianchang / 2.0, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 1, 0, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    
    // add cube face 5
    ct = CATransform3DMakeTranslation(-bianchang / 2.0, 0, 0);
    ct = CATransform3DRotate(ct, -M_PI_2, 0, 1, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    
    // add cube face 6
    ct = CATransform3DMakeTranslation(0, 0, -bianchang / 2);
    ct = CATransform3DRotate(ct, M_PI, 0, 1, 0);
    [cube addSublayer:[self faceWithTransform:ct]];
    
    // center the cube layer within the container
    CGSize containerSize = self.containerView.bounds.size;
    cube.position = CGPointMake(containerSize.width / 2.0, containerSize.height / 2.0);
    
    // apply the transform and return
    cube.transform = transform;
    return cube;
}

@end
